package com.techthinker.dreamBuilder.Dictionary;

import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.techthinker.dreamBuilder.R;
import com.techthinker.dreamBuilder.Entity.Dictionary;

public class DictionaryAdapter extends ArrayAdapter<Dictionary> {

	public  int NUM_DICTIONARY = 0;
	private final int TYPE_ITEM = 0;
	private final int TYPE_FOOTER = 1;
	private final int TYPE_HEADER = 2;
	private Resources res;

	private LayoutInflater mInflater;

	public DictionaryAdapter(Context context, int resource,
			int textViewResourceId, List<Dictionary> dictionaries) {
		super(context, resource, textViewResourceId, dictionaries);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		NUM_DICTIONARY=dictionaries.size()-2;
		res=context.getResources();
	}

	@Override
	public int getItemViewType(int position) {
		if (position == 0) {
			return TYPE_HEADER;
		} else if (position == NUM_DICTIONARY + 1) {
			return TYPE_FOOTER;
		} else {
			return TYPE_ITEM;
		}
	}

	@Override
	public int getViewTypeCount() {
		return 3;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		DictionaryViewHolder holder = null;
		final Dictionary dictionary = getItem(position);
		switch (getItemViewType(position)) {
		case TYPE_ITEM:
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.dic_list_item, null);
				holder = new DictionaryViewHolder();
				holder.name = (TextView) convertView
						.findViewById(R.id.tv_dic_name);
				holder.version = (TextView) convertView
						.findViewById(R.id.tv_dic_version);
				holder.listsNum = (TextView) convertView
						.findViewById(R.id.tv_dic_listsNum);
				holder.wordsNum = (TextView) convertView
						.findViewById(R.id.tv_dic_wordsNum);
				holder.select = (CheckBox) convertView
						.findViewById(R.id.cb_dic_select);
				convertView.setTag(holder);
			} else {
				holder = (DictionaryViewHolder) convertView.getTag();
			}

			holder.name.setText(dictionary.getName());
			holder.version.setText(res.getString(R.string.version)+dictionary.getVersion());
			holder.listsNum.setText(res.getString(R.string.listsNum)+String.valueOf(dictionary.getListsNum()));
			holder.wordsNum.setText(res.getString(R.string.wordsNum)+String.valueOf(dictionary.getWordsNum()));
			holder.select.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					//boolean s =buttonView.isChecked();
				}
			});
			break;
		case TYPE_HEADER:
			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.dic_bar_header, null);
				holder = new DictionaryViewHolder();
				holder.name = (TextView) convertView
						.findViewById(R.id.tv_dic_list_tittle);
				holder.name.setText(res.getString(R.string.dic_fromsd_titlebar));
				convertView.setTag(holder);
			} else {
				holder = (DictionaryViewHolder) convertView.getTag();
			}
			break;
		case TYPE_FOOTER:
			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.dic_bar_botton, null);
				holder = new DictionaryViewHolder();
				convertView.setTag(holder);
			} else {
				holder = (DictionaryViewHolder) convertView.getTag();
			}
			break;
		}
		return convertView;
	}

	public static class DictionaryViewHolder{
		public TextView name;
		public TextView version;
		public TextView listsNum;
		public TextView wordsNum;
		public ImageView icon;
		public CheckBox select;
	}

}
